package com.webserver.controllers;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ArticleController {
    private static String articlesDir ="./articles/";
    static{
        File articleDir =new File(articlesDir);
        if (!articleDir.exists()){
            articleDir.mkdirs();
        }
    }
    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("开始处理文章提交！！！！！！！！！！");
        String title =request.getParameter("title");
        String author =request.getParameter("author");
        String content =request.getParameter("content");
        if(title==null || author==null || content==null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }
        File articleFile =new File(articlesDir + title + ".obj");
        if (articleFile.exists()){
            File file =new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }try (
                FileOutputStream fos = new FileOutputStream(articleFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            Article article = new Article(title,author,content);
            oos.writeObject(article);
            //3设置注册成功页面
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @RequestMapping("/myweb/showAllArticle")
    public void  showAllArticle(HttpRequest request,HttpResponse response){
        System.out.println("开始处理文章列表");

        List<Article> articlesList =new ArrayList<>();
        File dir =new File(articlesDir);
        File[] subs =dir.listFiles(f->f.getName().endsWith(".obj"));
        for ( File articleFile :subs){
            try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(articleFile));) {
                Article article =(Article) ois.readObject();
                articlesList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(articlesList);

        //2将用户拼接到一个html的表格中，每个用户拼接一个<tr>

        PrintWriter pw =response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for (Article article :articlesList) {
            pw.println("<tr>");
            pw.println("<td><a href=\"./showArticle?title="+article.getTitle()+"\">"+article.getTitle()+"</a></td>");
            pw.println("<td>"+article.getAuthor()+"</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        //拼接页面后，设置响应头
        response.setContentType("text/html");

    }
    @RequestMapping("/myweb/showArticle")
    public void showArticle(HttpRequest request,HttpResponse response) {
        System.out.println("处理显示一篇文章!!!!!!!!");
        String title = request.getParameter("title");

        File file = new File(articlesDir + title + ".obj");
        if (file.exists()) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(file)
                    )
            ) {
                Article article = (Article) ois.readObject();

                PrintWriter pw = response.getWriter();
                pw.println("<!DOCTYPE html>");
                pw.println("<html lang=\"en\">");
                pw.println("<head>");
                pw.println("<meta charset=\"UTF-8\">");
                pw.println("<title>" + article.getTitle() + "</title>");
                pw.println("</head>");
                pw.println("<body>");
                pw.println("<center>");
                pw.println("<h1>" + article.getTitle() + "</h1>");
                pw.println("<h3>" + article.getAuthor() + "</h3>");
                pw.println(article.getContent());
                pw.println("</center>");
                pw.println("</body>");
                pw.println("</html>");

                //拼接页面后,设置响应头
                response.setContentType("text/html");


            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        }
    }